<?php
namespace Article\Controller;

class IndexController extends \Common\Controller\BaseController {

	public $article,$category;

	public function _initialize(){
		parent::_initialize();
		$this->article = D('Article');
		$this->category = D('ArticleCate');
	}

    public function index(){
    	$tid = I('tid','1','intval');
    	$map['cateid'] = $tid;

        //栏目类别信息
        $category = $this->category->where($map)->find();

		$count = $this->article->where($map)->count();
		$page = new \Com\Page($count,$this->pagesize);

		$limit = $page->firstRow.",".$page->listRows;
		$order = "articleid desc";

		$list = $this->article->where($map)->order($order)->limit($limit)->select();
        $first = $this->article->order("articleid asc")->find();

        $articleinfo = $this->article->where(array(
            'cateid'  => $tid,
        ))->order('articleid desc')->find();

		$data = array(
			'list'   => $list,
			'page'   => $page->show(),
            'first'  => $first,
            'category' => $category,
		);
        $this->assign($articleinfo);
        $this->assign($data);
		$this->display();
    }

    //资讯详情
    public function show(){
    	$id = I('id','','intval');

    	if ($id == '') {
    		$this->error("非法操作！");
    	}

    	$data = $this->article->where(array(
    		'articleid'  => $id,
    	))->find();

        //栏目类别信息
        $category = $this->category->where(array(
            'cateid'  => $data['cateid']
        ))->find();

        $list = $this->article->where(array(
            'cateid'  => $data['cateid']
        ))->order("articleid desc")->select();
        $this->assign('list',$list);
    	//更新统计
    	$this->article->where(array(
    		'articleid'  => $id,
    	))->setInc('count_view');
    	$breadcrumb = array(
    		'bread_cate' => array(
    			'name'   => $category['catename'],
    			'url'    => U('Article/Index/index',array('tid'=>$data['cateid'])),
    		),
    		'bread_end' => array(
    			'name'   => $data['title'],
    			'url'    => '',
    		),
    	);
        $this->assign('category',$category);
    	$this->assign($breadcrumb);
    	$this->assign($data);
    	$this->display();
    }
}